import { request } from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  data: {
    tabs:[
      {
        id:0,
        value:"综合",
        isActive:true

      },
      {
        id:1,
        value:"销量",
        isActive:false

      },
      {
        id:2,
        value:"价格",
        isActive:false

      }
    ],
    goodsList:[],
  },
  QueryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  // 总页数
  totalPages:1,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      this.QueryParams.cid = options.cid

      this.getGoodsList()
      
  },

  //获取商品数据函数
   async getGoodsList(){
    const res=await request({url:'/goods/search',data:this.QueryParams});
    // 获取总条数
    const total =  res.data.message.total
    this.totalPages = Math.ceil(total/this.QueryParams.pagesize);
  
    this.setData({
      goodsList:[...this.data.goodsList,...res.data.message.goods]
    })
    //关闭下拉刷新
    wx.stopPullDownRefresh()
  },

  //下拉触底事件
  onReachBottom(){
      if(this.QueryParams.pagenum>=this.totalPages){
          //提示没有数据了
          wx.showToast({title: '已经到底了'});
            
      }else{
          // 刷新下一页
          this.QueryParams.pagenum++
          this.getGoodsList()
      }
  },

  // 下拉刷新事件
  onPullDownRefresh(){
    //清空数组
    this.setData({
      goodsList:[]
    })
    //重置当前页数
    this.QueryParams.pagenum=1
    //发送请求
    this.getGoodsList()
  },

  //点击事件
  handleTabsItemChange(e){
  //  获取被点击的索引
 const {index} = e.detail;
 let {tabs} =this.data;
 tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
 this.setData({
   tabs
 })
  }
})